/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
 */
package View;

import javax.swing.JFileChooser;
import View.Model.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;

/**
 *
 * @author emwma
 */
public class OpenProject extends javax.swing.JFrame {

    /**
     * Creates new form OpenProject
     */
    Project project;
    Server server;
    Protocol protocol;
    Thing thing;
    Topic topic;
    public OpenProject(Project project, Server server, Protocol protocol, Thing thing, Topic topic) {
        initComponents();
        this.project = project;
    }
    
    private static String directoryPath;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtProjectFolder = new javax.swing.JTextField();
        btnOk = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnBrowser = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Project Folder:");

        btnOk.setText("OK");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnBrowser.setText("Browser");
        btnBrowser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtProjectFolder, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBrowser)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtProjectFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBrowser))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOk)
                    .addComponent(btnCancel))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBrowserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowserActionPerformed
        // TODO add you code here:
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int dialogVal = jfc.showOpenDialog(null);
        if (dialogVal == JFileChooser.APPROVE_OPTION){
            directoryPath = jfc.getSelectedFile().getAbsolutePath();
            System.out.println(directoryPath);
            txtProjectFolder.setText(directoryPath);
        }
        
    }//GEN-LAST:event_btnBrowserActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        super.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
        String aux = directoryPath + "/project.json";
        String conteudo = "";
        
        try {
            conteudo = new String(Files.readAllBytes(Paths.get(aux)));
        } catch (IOException ex) {
            Logger.getLogger(OpenProject.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        JSONObject objetoJson = new JSONObject(conteudo);
        
   
        //JSONObject objetoJson = jsonArray.getJSONObject(x);
        project.setProjectNome(objetoJson.getString("projectNome"));
        project.setProjectFolder(objetoJson.getString("projectFolder"));
        
        aux = directoryPath + "/server.json";
        
        try {
            conteudo = new String(Files.readAllBytes(Paths.get(aux)));
        } catch (IOException ex) {
            Logger.getLogger(OpenProject.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        objetoJson = new JSONObject(conteudo);
        
        server.setNomeServer(objetoJson.getString("nomeServer"));
        server.setPasswordServer(objetoJson.getString("passwordServer"));
        server.setIdServer(objetoJson.getString("idServer"));
        server.setPortaServer(objetoJson.getString("portaServer"));
        
        aux = directoryPath + "/protocol.json";
        
        try {
            conteudo = new String(Files.readAllBytes(Paths.get(aux)));
        } catch (IOException ex) {
            Logger.getLogger(OpenProject.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        objetoJson = new JSONObject(conteudo);
        
        protocol.setNomeProt(objetoJson.getString("nomeProt"));
        protocol.setUserProt(objetoJson.getString("userProt"));
        protocol.setPasswordProt(objetoJson.getString("passwordProt"));
        
        aux = directoryPath + "/thing.json";
        
        try {
            conteudo = new String(Files.readAllBytes(Paths.get(aux)));
        } catch (IOException ex) {
            Logger.getLogger(OpenProject.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        objetoJson = new JSONObject(conteudo);
        
        thing.setNome(objetoJson.getString("nome"));
        thing.setDescricao(objetoJson.getString("descricao"));
        
        super.dispose();
        
        aux = directoryPath + "/topic.json";
        
        try {
            conteudo = new String(Files.readAllBytes(Paths.get(aux)));
        } catch (IOException ex) {
            Logger.getLogger(OpenProject.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        objetoJson = new JSONObject(conteudo);
        
        thing.setNome(objetoJson.getString("nome"));
        thing.setDescricao(objetoJson.getString("descricao"));
        
        super.dispose();
    }//GEN-LAST:event_btnOkActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBrowser;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtProjectFolder;
    // End of variables declaration//GEN-END:variables
}
